// src\main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { permission, role } from './directives/permission'


// ========== 在创建Vue实例之前添加 ==========
const initApp = () => {
  // 清理无效的localStorage数据
  ['permissions', 'roles', 'token'].forEach(key => {
    const value = localStorage.getItem(key)
    if (value === '' || value === 'null' || value === 'undefined') {
      localStorage.removeItem(key)
    }
  })

  // 创建Vue应用实例
  const app = createApp(App)
  const pinia = createPinia()
  
  // 注册权限指令
  app.directive('permission', permission)
  app.directive('role', role)
  
  // 注册所有图标
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }
  
  // 正常初始化流程
  app.use(pinia)
     .use(router)
     .use(ElementPlus, {
       locale: zhCn,
     })
     .mount('#app')
}

// 启动应用
initApp()